import { getToken } from '@/utils/cookie'
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/forum/login",
    },
    {
      path: "/forum/login",
      name: "login",
      component: () => import("../views/Login.vue"),
    },
    {
      path: '/forum/system',
      name: 'system',
      component: () => import("../views/System.vue"),
    },
    {
      path: '/forum/layout',
      name: 'layout',
      component: () => import("../views/Layout.vue"),
      children: [
        {
          path: 'cuser',
          name: 'cuser',
          component: () => import("../views/Cuser.vue"),
        },
        {
          path: 'label',
          name: 'label',
          component: () => import("../views/Label.vue"),
        },
        {
          path: 'post',
          name: 'post',
          component: () => import("../views/Post.vue"),
        },
        {
          path: 'comment',
          name: 'comment',
          component: () => import("../views/Comment.vue"),
        },
        {
          path: 'interview',
          name: 'interview',
          component: () => import("../views/Interview.vue"),
        },
        {
          path: 'report',
          name: 'report',
          component: () => import("../views/Report.vue"),
        },
        {
          path: 'role',
          name: 'role',
          component: () => import("../views/Role.vue"),
        },
        {
          path: 'auth',
          name: 'auth',
          component: () => import("../views/Auth.vue"),
        },
        {
          path: 'settings',
          name: 'settings',
          component: () => import("../views/Settings.vue"),
        },
        {
          path: 'feedback',
          name: 'feedback',
          component: () => import("../views/Feedback.vue"),
        }
      ]
    }
  ]
})

router.beforeEach((to, form, next) => {
  if (getToken()) {
    if (to.path === '/forum/login') {
      next({ path: '/forum/layout/cuser' })
    } else {
      next()
    }
  } else {
    if (to.path !== '/forum/login') {
      next({
        path: '/forum/login'
      })
    } else {
      next()
    }
  }
})


export default router